//
//  ChatComplaintViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/21.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ChatComplaintViewController.h"
#import "ChatComplaintCell.h"

@interface ChatComplaintViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end

@implementation ChatComplaintViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"投诉";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 56, 26)];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    rightButton.backgroundColor = [UIColor primaryColor];
    rightButton.layer.cornerRadius = 5;
    rightButton.layer.masksToBounds = YES;
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] getChatComplaint] subscribeNext:^(ChatComplaintList * _Nullable chatComplaintList) {
        @strongify(self);
        [self.dataArr addObjectsFromArray:chatComplaintList.dataList];
        for (ChatComplaint *model in chatComplaintList.dataList) {
            model.isSelect = NO;
        }
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataArr.count;
    } else if (section == 1){
        return 1;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ChatComplaintCell *cell = [ChatComplaintCell cellWithTableView:tableView];

        [cell configComplaintTypeCellWithModel:self.dataArr[indexPath.row]];
        return cell;
    } else if (indexPath.section == 1) {
        ChatComplaintPhotoCell *cell = [ChatComplaintPhotoCell cellWithTableView:tableView];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ChatComplaint *model = self.dataArr[indexPath.row];
        model.isSelect = !model.isSelect;
        [self.myTableView reloadData];
    }
}
#pragma mark - target action
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightButtonClick {
    NSMutableArray *selectTypeArr = [[NSMutableArray alloc] init];
    for (ChatComplaint *model in self.dataArr) {
        if (model.isSelect) {
            [selectTypeArr addObject:model.value];
        }
    }
    ChatComplaintParam *param = [[ChatComplaintParam alloc] init];
    param.complaintType = selectTypeArr;
    param.memberNum = [UserInfoDataProvider sharedInstance].userInfo.memberNum;
    param.toMemberNum = self.identifier;
    param.content = @"123";
    @weakify(self);
    [[[PigMarketApi sharedInstance] chatComplaintWithParam:param] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        [self toastWithContent:@"投诉成功"];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
    
}
#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}


@end
